<#escape x as x?html>
<div class="box box-solid box-fusion no-shadow no-border appList">
    <div class="box-header   hide">
        <div class="col-md-7">
            <h3 class="box-title">&nbsp;</h3>
        </div>
        <div class="box-tools">
            <form class="hide" id="listForm" method="post">
                <input id="pageIndex" name="pageIndex" value="${page.pageIndex!''}">
                <input id="pageSize" name="pageSize" value="${page.pageSize!''}">
                <input id="pagesNum" name="pagesNum" value="${page.pagesNum!''}">
            </form>
            <@y.htmlFuzzySearchListForm>
                <input id="state" name="state" value="${state!}">
            </@y.htmlFuzzySearchListForm>
        </div>
    </div><!-- /.box-header -->
    <#if state == "TEST">
        <div class="box-header">
            <div class="col-md-7">
                <h3 class="box-title">&nbsp;</h3>
            </div>
            <div class="box-tools">
                <div class="input-group-btn">
                    <button class="btn btn-default btn-sm pull-right action-insert">
                        <i class="fa fa-plus"> <@s.message "page.insert"/></i>
                    </button>
                </div>
            </div>
        </div><!-- /.box-header -->
    </#if>
    <div class="box-body table-responsive no-padding">
        <table class="table table-inner-bordered table-hover with-border-top"
               data-form-name="应用">
            <thead>
            <th class="col-md-1 col-lg-1">
                名称
            </th>
            <th class="col-md-1 col-lg-1">
                版本
            </th>
            <th class="col-md-1 col-lg-1">
                Context
            </th>
            <th class="col-md-1 col-lg-1">
                分类
            </th>
            <th class="col-md-1 col-lg-1">
                订阅数
            </th>
            <th class="col-md-1 col-lg-2">
                创建时间
            </th>
            <th class="col-md-1 col-lg-2">
                更新时间
            </th>
            <th class="col-md-3 col-lg-3">
                操作
            </th>
            </thead>
            <tbody>
                <#if (page.entities??)&&(page.entities?size>0)>
                    <#list page.entities as record>
                    <tr record-id='${record.id}'>
                        <td>
                        ${(record.name)!''}
                        </td>
                        <td>
                        ${(record.version)!''}
                        </td>
                        <td>
                        ${(record.context)!''}
                        </td>
                        <td>
                        ${(record.category.name)!''}
                        </td>
                        <td>
                        ${(record.subscribeCount)!''}
                        </td>
                        <td>
                        ${(record.createTime)!''}
                        </td>
                        <td>
                        ${(record.updateTime)!''}
                        </td>
                        <td class="popup-close">
                            <#if state == "TEST">
                                <button class="btn btn-default btn-sm action-edit">
                                    <i class="fa fa-hand-paper-o"> <@s.message "page.edit"/></i>
                                </button>
                            </#if>
                            <#if state != "PUBLISHED">
                                <button class="btn btn-default btn-sm action-delete">
                                    <i class="fa fa-trash-o"> <@s.message "page.delete"/></i>
                                </button>
                            </#if>
                            <#if state != "DEPRECATED" && state!="APPLYING">
                                <button class="btn btn-default btn-sm expired">
                                    <i class="fa fa-caret-square-o-down"> <#if state == "TEST">过期作废<#else>过期下架</#if></i>
                                </button>
                            </#if>
                            <#if state != "DEPRECATED">
                                <button class="btn btn-default btn-sm action-open-url">
                                    <i class="fa fa fa-eye"> 测试</i>
                                </button>
                            </#if>
                            <#if state == "TEST">
                                <button class="btn btn-default btn-sm apply-publish">
                                    <i class="fa fa-hand-paper-o"> 申请发布</i>
                                </button>
                            </#if>
                            <#if state == "PUBLISHED" || state == "APPLYING">
                                <button class="btn btn-default btn-sm cancel-publish">
                                    <i class="fa fa-hand-paper-o"> 取消发布</i>
                                </button>
                            </#if>
                        </td>
                    </tr>
                    </#list>
                </#if>
            </tbody>
        </table>
    </div><!-- /.box-body -->
    <div class="box-footer no-border clearfix">
        <@y.htmlPaginationOverview/>
    </div>
</div><!-- /.box -->
    <@y.htmlModal type="save" width="750px"/>
<script>
    context.call([
        "list",
        "tools",
        "core"
    ], function (list, tools, core) {
        function updateTree() {
            reloadTree({keyCode: 13}, function () {
                core.jumpToTree("${state?lower_case}App");
            });
        }

        list.initSearchList("/console/app", {
            recordIdName: "record-id",
            actionEdit: "record-id",
            targetSelector: ".appList",
            urlList: "list/${state!}",
            searchFormSelector: "#listForm",
            recordFormSelector: "#recordForm",
            recordAttr: "record-attr",
            recordAttr2: "record-attr2",
            urlInsert: "insertApplication",
            urlUpdate: "updateApplication",
            insertAction: updateTree,
            updateAction: updateTree,
            deleteAction: updateTree
        });

        function getRecordId() {
            return $(this).closest("[record-id]").attr("record-id");
        }


        $(".apply-publish").each(function () {
            $(this).on("click", function (e) {
                var appId = getRecordId.call($(this));
                tools.confirm({
                    message: "发布应用?",
                    title: "发布应用",
                    callback: function (result) {
                        if (result) {
                            $.ajax({
                                url: "/console/app/publish?id=" + appId,
                                success: function (msg) {
                                    if (core.message(msg)) {
                                        core.loadContent("/console/app/list/TEST");
                                    }
                                    updateTree();
                                    tools.notifyMini({message: msg.info});
                                }
                            })
                        }
                    }
                });
            });
        });

        $(".expired").each(function () {
            $(this).on("click", function (e) {
                var appId = getRecordId.call($(this));
                tools.confirm({
                    message: "下架应用?",
                    title: "下架应用",
                    callback: function (result) {
                        if (result) {
                            $.ajax({
                                url: "/console/app/retire?id=" + appId,
                                success: function (msg) {
                                    if (core.message(msg)) {
                                        core.loadContent("/console/app/list/TEST");
                                    }
                                    updateTree();
                                    tools.notifyMini({message: msg.info});
                                }
                            })
                        }
                    }
                });
            });
        });

        $(".cancel-publish").each(function () {
            $(this).on("click", function (e) {
                var appId = getRecordId.call($(this));
                tools.confirm({
                    message: "取消发布应用?",
                    title: "取消发布",
                    callback: function (result) {
                        if (result) {
                            $.ajax({
                                url: "/console/app/cancelPublish?id=" + appId,
                                success: function (msg) {
                                    if (core.message(msg)) {
                                        core.loadContent("/console/app/list/PUBLISHED");
                                    }
                                    updateTree();
                                    tools.notifyMini({message: msg.info});
                                }
                            })
                        }
                    }
                });
            });
        });

        $(".action-open-url").on("click", function () {
            var appId = getRecordId.call($(this));
            $.ajax({
                url: "/console/app/rest/appUrl/" + appId,
                success: function (url) {
                    window.open(url);
                }
            });
        });
    });
</script>
</#escape>